package dearchenpush.good.com.qianfanlive.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.modules.discover.DiscoverFragment;
import dearchenpush.good.com.qianfanlive.modules.live.fragment.LiveFragment;
import dearchenpush.good.com.qianfanlive.modules.mine.MineFragment;
import dearchenpush.good.com.qianfanlive.modules.rank.fragment.RankFragment;
import dearchenpush.good.com.qianfanlive.modules.show.ShowFragment;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private FragmentManager manager;
    private List<Fragment> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index=0;
                for (int i = 0; i < radioGroup.getChildCount(); i++) {

                    RadioButton childAt = (RadioButton) radioGroup.getChildAt(i);
                    if (childAt.isChecked()) {
                        index = i;
                        break;

                    }
                }
                showFragment(index);
            }
        });
    }

    private void showFragment(int index) {
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < data.size(); i++) {
            if (i == index) {
                transaction.show(data.get(index));
            } else {
                transaction.hide(data.get(i));
            }

        }
        transaction.commit();

    }

    private void setData() {
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < data.size(); i++) {
            transaction.add(R.id.activity_main_container, data.get(i), i + "");
            transaction.hide(data.get(i));
        }
        transaction.show(data.get(0));
        transaction.commit();

    }

    private void initData() {
        data.add(LiveFragment.newInstance());
        data.add(RankFragment.newInstance());
        data.add(ShowFragment.newInstance());
        data.add(DiscoverFragment.newInstance());
        data.add(MineFragment.newInstance());
    }


    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.activity_main_radioGroup);
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        manager = getSupportFragmentManager();
    }
}
